﻿Imports BUS
Imports DTO
Imports System.Drawing.Printing

Public Class frmTraCuuMachDieuKhien

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dgvDuLieuThem.Rows.Clear()
        Dim dp As New MachDieuKhienBUS
        Dim ds As List(Of MachDieuKhienDTO) = dp.layDSMachDK()

        For Each obj As MachDieuKhienDTO In ds
            Dim TenLoai As String = (New LoaiMachDieuKhienBUS).layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
            Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
            dgvDuLieuThem.Rows.Add(row)
        Next
    End Sub

    Private Sub frmTraCuuMachDieuKhien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dp As New LoaiMachDieuKhienBUS
        'cbLoaiMach.DataSource = dp.layDSLoaiMDK()
        Dim ds As List(Of LoaiMachDieuKhienDTO) = dp.layDSLoaiMDK()
        Dim l As New LoaiMachDieuKhienDTO
        l.MaLoai = 0
        l.TenLoai = "Tất cả"
        ds.Insert(0, l)
        With cbLoaiMach
            .DataSource = ds
            .DisplayMember = "TenLoai"
            .ValueMember = "MaLoai"
            .SelectedIndex = 0
        End With
        'cbLoaiMach.Items.Add("")
        gpbTimKiemNC.Enabled = False
        chbTimKiemNC.Checked = False
        chbChinhSuaCSDL.Checked = False
        btnSua.Enabled = False
        btnXoa.Enabled = False
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        txtDongDien.Text = Nothing
        txtHangSX.Text = Nothing
        txtTenMach.Text = Nothing
        txtDongDien2.Text = Nothing
        txtGia.Text = Nothing
        txtGia2.Text = Nothing
        txtHSTangThem.Text = Nothing
        txtHSTangThem2.Text = Nothing
        txtMaMach.Text = Nothing
        txtSoCongPin.Text = Nothing
        txtSoCongPin2.Text = Nothing
        txtTenMach.Text = Nothing
        cbLoaiMach.SelectedIndex = 0

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If (dgvDuLieuThem.SelectedRows.Count > 0) Then
            Try
                For Each row As DataGridViewRow In dgvDuLieuThem.SelectedRows
                    Dim dp As New MachDieuKhienBUS
                    Dim maMach As Integer = Integer.Parse(row.Cells("MaMach").Value.ToString())
                    Dim ret = dp.xoaMachDK(maMach)
                    If (ret = 1) Then
                        dgvDuLieuThem.Rows.Remove(row)
                    End If
                Next
                MessageBox.Show("Xoa thanh cong")
            Catch ex As Exception
                MessageBox.Show("Loi: " & ex.Message)
            End Try
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If (dgvDuLieuThem.SelectedRows.Count > 0) Then
            Dim ids As New List(Of Integer)
            For Each row As DataGridViewRow In dgvDuLieuThem.SelectedRows
                ids.Add(Integer.Parse(row.Cells("MaMach").Value.ToString()))
            Next
            Dim frm As New frmLuuMachDieuKhien
            frm.addList(ids)
            frm.MdiParent = Me.MdiParent
            frm.Show()
        End If
    End Sub

    Private Sub chbTimKiemNC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbTimKiemNC.CheckedChanged
        Dim chb As CheckBox = DirectCast(sender, CheckBox)
        If (chb.Checked) Then
            gpbTimKiemNC.Enabled = True
        Else
            gpbTimKiemNC.Enabled = False
        End If
    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gpbTimKiemNC.Enter

    End Sub

    Private Sub chbChinhSuaCSDL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbChinhSuaCSDL.CheckedChanged
        Dim chb As CheckBox = DirectCast(sender, CheckBox)
        If (chb.Checked) Then
            btnSua.Enabled = True
            btnXoa.Enabled = True
        Else
            btnSua.Enabled = False
            btnXoa.Enabled = False
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim dp As New MachDieuKhienBUS
        dgvDuLieuThem.Rows.Clear()
        If (Not txtMaMach.Text Is Nothing And txtMaMach.Text.Length > 0) Then
            Dim id As Integer
            If (Integer.TryParse(txtMaMach.Text, id)) Then
                Dim obj As MachDieuKhienDTO = dp.layMachDK(id)
                If (Not obj Is Nothing) Then

                    Dim TenLoai As String = (New LoaiMachDieuKhienBUS).layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
                    Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
                    dgvDuLieuThem.Rows.Add(row)
                End If
            End If
        Else
            Dim ds As List(Of MachDieuKhienDTO)
            ds = dp.timKiemNC(txtTenMach.Text, cbLoaiMach.SelectedValue, txtHangSX.Text)

            If (Not chbTimKiemNC.Checked) Then

                For Each obj As MachDieuKhienDTO In ds
                    Dim TenLoai As String = (New LoaiMachDieuKhienBUS).layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
                    Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
                    dgvDuLieuThem.Rows.Add(row)
                Next
            Else
                If (txtDongDien.Text.Length > 0) Then
                    Dim dongDien As Integer
                    If (Integer.TryParse(txtDongDien.Text, dongDien)) Then
                        For i As Integer = 0 To ds.Count - 1

                            If (ds(i).DongDien < dongDien) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtDongDien2.Text.Length > 0) Then
                    Dim dongDien As Integer
                    If (Integer.TryParse(txtDongDien2.Text, dongDien)) Then
                        For i As Integer = 0 To ds.Count - 1

                            If (ds(i).DongDien > dongDien) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtHSTangThem.Text.Length > 0) Then
                    Dim value As Integer
                    If (Integer.TryParse(txtHSTangThem.Text, value)) Then
                        For i As Integer = 0 To ds.Count - 1

                            If (ds(i).HieuSuatTangThem < value) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtHSTangThem2.Text.Length > 0) Then
                    Dim hsTangThem As Integer
                    If (Integer.TryParse(txtHSTangThem2.Text, hsTangThem)) Then
                        For i As Integer = 0 To ds.Count - 1

                            If (ds(i).HieuSuatTangThem > hsTangThem) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtSoCongPin.Text.Length > 0) Then
                    Dim soCongPin As Integer
                    If (Integer.TryParse(txtSoCongPin.Text, soCongPin)) Then
                        For i As Integer = 0 To ds.Count - 1
                            If (ds(i).SoCongPin < soCongPin) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtSoCongPin2.Text.Length > 0) Then
                    Dim soCongPin As Integer
                    If (Integer.TryParse(txtSoCongPin2.Text, soCongPin)) Then
                        For i As Integer = 0 To ds.Count - 1
                            If (ds(i).SoCongPin > soCongPin) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If


                If (txtGia.Text.Length > 0) Then
                    Dim gia As Integer
                    If (Integer.TryParse(txtGia.Text, gia)) Then
                        For i As Integer = 0 To ds.Count - 1
                            If (ds(i).Gia < gia) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If
                End If

                If (txtGia2.Text.Length > 0) Then
                    Dim gia As Integer
                    If (Integer.TryParse(txtGia2.Text, gia)) Then
                        For i As Integer = 0 To ds.Count - 1
                            If (ds(i).Gia > gia) Then
                                ds.RemoveAt(i)
                                i = i - 1
                            End If
                        Next
                        If (ds.Count = 0) Then
                            Return
                        End If
                    End If

                End If

                For Each obj As MachDieuKhienDTO In ds
                    Dim TenLoai As String = (New LoaiMachDieuKhienBUS).layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
                    Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
                    dgvDuLieuThem.Rows.Add(row)
                Next

            End If
        End If
    End Sub
    Dim MyDataGridViewPrinter As DataGridViewPrinter
    Private Function SetupThePrinting() As Boolean
        If PrintDialog.ShowDialog() <> DialogResult.OK Then
            Return False
        End If

        PrintDocument.DocumentName = "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp"
        PrintDocument.PrinterSettings = PrintDialog.PrinterSettings
        PrintDocument.DefaultPageSettings = PrintDialog.PrinterSettings.DefaultPageSettings
        PrintDocument.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)

        If MessageBox.Show("Canh chính giữa trang?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvDuLieuThem, PrintDocument, True, True, "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        Else
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvDuLieuThem, PrintDocument, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        End If
        Return True
    End Function

    Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
        'PrintPreviewDialog1.Document = PrintDocument1
        'PrintDocument1.DefaultPageSettings.Landscape = True
        'PrintPreviewDialog1.ShowDialog()
        If SetupThePrinting() Then
            PrintDocument.Print()
        End If

    End Sub

    Private Sub btnXemTruoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemTruoc.Click
        If SetupThePrinting() Then
            PrintPreviewDialog.Document = PrintDocument
            PrintPreviewDialog.ShowDialog()
        End If
    End Sub

    Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
        Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If more = True Then
            e.HasMorePages = True
        End If
    End Sub
End Class